package com.heima.article.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.mapper.AuthorMapper;
import com.heima.article.service.GeneratePageService;
import com.heima.common.exception.CustException;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.enums.AppHttpCodeEnum;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;

@Slf4j
@Service
public class GeneratePageServiceimpl implements GeneratePageService {
    @Autowired
    @Qualifier("minIOFileStorageService")
    FileStorageService fileStorageService;
    @Autowired
    //作者
    private AuthorMapper authorMapper;
    @Autowired //文章
    private ApArticleMapper apArticleMapper;
    @Autowired
    //配置对象
    private Configuration configuration;
    @Value("${file.minio.prefix}")
    private String prefix;

    /**
     * @param content   文章详情内容
     * @param apArticle 文章对象
     */
    @Override
    public void generatePage(String content, ApArticle apArticle) {
        //1.获取文章内容
        try {
            //2.模板
            //模板对象
            Template template = configuration.getTemplate("article.ftl");
            //3.数据
            HashMap<String, Object> parms = new HashMap<>();
            //文章详细
            parms.put("content", JSONArray.parseArray(content));
            //文章信息
            parms.put("article", apArticle);
            ApAuthor apAuthor = authorMapper.selectById(apArticle.getAuthorId());
            if (apAuthor == null) {
                CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "作者信息不存在");
            }
            parms.put("authorApUserId", apAuthor.getUserId());
            //3.替换模板,并输出到指定地方
            StringWriter writer = new StringWriter();
            template.process(parms, writer);
            //将字符内容转化为输入流
            InputStream stream = new ByteArrayInputStream(writer.toString().getBytes());
            //4.上传到minio中,得到路径修改aparticle==>staric_url字段
            String storePath = fileStorageService.store(prefix, apArticle.getId() + ".html", "text/html", stream);
            apArticle.setStaticUrl(storePath);
            apArticleMapper.updateById(apArticle);

        } catch (IOException e) {
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR, "页面静态化失败,原因: 未找到模板" + e.getMessage());
        } catch (TemplateException e) {
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR, "页面静态化失败,原因: 模板替换失败" + e.getMessage());
        }


    }
}
